import { ref } from 'vue'
import { adapter, request } from '@/request'
import { promotionDetailByGet } from '@/core/api/promotion'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { Creative, Promotion } from '@/core/interfaces/promotion'
import { ProductEnum } from '@/core/enums/platform'

export interface PromotionDetail extends Promotion {
  creatives: Creative[]
  version?: ProductEnum
}

interface Params {
  adId: string
}

export default function getPromotionDetail() {
  const service = adapter<RequestWrapper<PromotionDetail>>(request)
  const loading = ref(false)
  const response = ref<Partial<PromotionDetail>>({})

  const fetch = (params: Params) => {
    const { url, method } = promotionDetailByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
